package com.sywb.chuangyebao.contract;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.IncomeExpenditureBean;
import com.sywb.chuangyebao.utils.DoubleFormatUtil;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;

import org.bining.footstone.adapter.BaseRecyclerAdapter;
import org.bining.footstone.adapter.BaseRecyclerDivider;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.utils.ScreenUtils;

import java.util.List;

public interface IncomeAndDependitureContract {
    interface View extends BaseRefreshContract.View {
        int getTabIndex();
    }

    class Presenter extends BaseRefreshContract.Presenter<View> {

        private IncomeExpenditureAdapter incomeExpenditureAdapter = null;
        private int pageNo = 1;

        @Override
        public void onStart() {
            super.onStart();
            incomeExpenditureAdapter = new IncomeExpenditureAdapter(mContext);
            setRecyclerAdapter(incomeExpenditureAdapter);
            initData();
        }

        @Override
        public void initData() {
            super.initData();
            onStartAsync();
            pageNo = 1;
            listIncomeAndExpenditure();
        }

        @Override
        public void onStartRefresh() {
            super.onStartRefresh();
            pageNo = 1;
            listIncomeAndExpenditure();
        }

        @Override
        public void onStartLoadMore() {
            super.onStartLoadMore();
            pageNo++;
            listIncomeAndExpenditure();
        }

        /**
         * 获取收支明细
         */
        private void listIncomeAndExpenditure(){
            if (mView.getTabIndex()==0){
                getDatas(Constants.INCOME_EXPENDITURE);
            }else if (mView.getTabIndex()==1){
                getDatas(Constants.INCOME);
            }else if (mView.getTabIndex()==2){
                getDatas(Constants.EXPENDITURE);
            }
        }

        private void getDatas(int type){
            HttpUtils.incomeAndExpenditure( pageNo, Constants.PAGESIZE,type,new HttpCallback<List<IncomeExpenditureBean>>() {
                @Override
                public void onSuccess(List<IncomeExpenditureBean> incomeExpenditureBeans) {
                    if (incomeExpenditureBeans!=null){
                        if (pageNo==1){
                            incomeExpenditureAdapter.clearDatas();
                            onEndRefresh();
                            if (incomeExpenditureBeans!=null&&incomeExpenditureBeans.size()>0){
                                incomeExpenditureAdapter.setFooterView(R.layout.layout_footer);
                            }else {
                                incomeExpenditureAdapter.setFooterView(null);
                            }
                        }else {
                            if (incomeExpenditureBeans!=null&&incomeExpenditureBeans.size()>=0){
                                incomeExpenditureAdapter.setFooterView(R.layout.layout_footer);
                            }else {
                                incomeExpenditureAdapter.setFooterView(null);
                            }
                        }
                        incomeExpenditureAdapter.notifyDataChangedAfterLoadMore(incomeExpenditureBeans);
                    }
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    if (pageNo == 1) {
                        onEndRefresh();
                    } else {
                        onEndLoadMore();
                    }
                    showMessage(strMsg);
                    onFinishAsync();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                    isNoData();
                }
            });
        }
        @Override
        public boolean useRecyclerDivider() {
            return false;
        }

        class IncomeExpenditureAdapter extends BaseRecyclerAdapter<IncomeExpenditureBean> {

            public IncomeExpenditureAdapter(Context context){
                super(context, R.layout.item_income_dependiture_layout);
            }
            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int i, IncomeExpenditureBean incomeDependitureBean) {
                incomeDependitureBean.getDateOrTime();
                viewHolderHelper.setText(R.id.item_withdraw_date_tv,incomeDependitureBean.getDateTime());
                // (1=投稿图片收入，2=投稿视频收入，3=投稿文章收入) 都是奖励金 4=支付宝提现 5=银行卡(暂时不用) 6 微信提现
                if (incomeDependitureBean.getChannel().equals(String.valueOf(4))){
                    viewHolderHelper.setText(R.id.item_withdraw_type_tv,R.string.alipay);
                }else if (incomeDependitureBean.getChannel().equals(String.valueOf(6))){
                    viewHolderHelper.setText(R.id.item_withdraw_type_tv,R.string.weixin_withdrawal);
                }else if(incomeDependitureBean.getChannel().equals(String.valueOf(5))){
                    viewHolderHelper.setText(R.id.item_withdraw_type_tv,"银行卡");
                }else {
                    viewHolderHelper.setText(R.id.item_withdraw_type_tv,"奖励金");
                }
                //0表示收入 1表示支出
                if (incomeDependitureBean.getCategory().equals(String.valueOf(0))){
                    viewHolderHelper.setText(R.id.item_withdraw_amount_tv,"+"+ DoubleFormatUtil.formatDoubleNumber(incomeDependitureBean.getAmt()));
                    viewHolderHelper.setTextColor(R.id.item_withdraw_amount_tv, ContextCompat.getColor(mContext, R.color.colorIncome));
                }else {
                    viewHolderHelper.setText(R.id.item_withdraw_amount_tv,"-"+DoubleFormatUtil.formatDoubleNumber(incomeDependitureBean.getAmt()));
                    viewHolderHelper.setTextColor(R.id.item_withdraw_amount_tv, ContextCompat.getColor(mContext, R.color.colorDependiture));
                }
                //状态 (1=待审核 2=待打款) 交易中 3=交易成功 4=交易失败
                if (incomeDependitureBean.getStatus().equals(String.valueOf(1))||incomeDependitureBean.getStatus().equals(String.valueOf(2))){
                    viewHolderHelper.setTextColor(R.id.item_withdraw_status_tv, ContextCompat.getColor(mContext, R.color.colorTradingStatus));
                    viewHolderHelper.setText(R.id.item_withdraw_status_tv,"交易中");
                }else if (incomeDependitureBean.getStatus().equals(String.valueOf(3))){
                    viewHolderHelper.setText(R.id.item_withdraw_status_tv,"交易成功");
                    viewHolderHelper.setTextColor(R.id.item_withdraw_status_tv, ContextCompat.getColor(mContext, R.color.colorGray));
                }else if (incomeDependitureBean.getStatus().equals(String.valueOf(4))){
                    viewHolderHelper.setText(R.id.item_withdraw_status_tv,"交易失败");
                    viewHolderHelper.setTextColor(R.id.item_withdraw_status_tv, ContextCompat.getColor(mContext, R.color.colorGray));
                }
            }
        }
    }

}
